package com.momoauth.api_client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.momoauth.api_client.entity.Role;
import com.momoauth.api_client.mapper.RoleMapper;
import com.momoauth.api_client.mapper.UserRoleMapper;
import com.momoauth.common.core.result.ResponseResurt;
import com.momoauth.common.security.annotation.RequiresRoles;
import com.momoauth.common.security.enumeration.Logical;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色")
@RequiresRoles(value = {"超级管理员","普通管理员"}, logical = Logical.OR)
public class RoleController {

    @Resource
    RoleMapper roleMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    /**
     * 角色列表
     * @return
     */
    @ApiOperation(value = "角色列表")
    @RequestMapping(value = "/getRoleList", method = RequestMethod.GET)
    public ResponseResurt getRoleList() {
        List<Role> roleList = roleMapper.getRoleList();
        return ResponseResurt.success().data(roleList);
    }

    /**
     * 创建角色
     * @param role
     * @return
     */
    @ApiOperation(value = "创建角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true),
            @ApiImplicitParam(name = "description", value = "描述", required = true),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @RequestMapping(value = "/createRole", method = RequestMethod.POST)
    public ResponseResurt createRole(Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete", 0);
        // 判断是否已达到最大新增数量
        long count = roleMapper.selectCount(queryWrapper);
        if (count >= 10) {
            return ResponseResurt.error().msg("已达到最大新增数量。");
        }
        // 执行新增操作
        int isInsert = roleMapper.insert(role);
        if (isInsert > 0) {
            return ResponseResurt.success().msg("恭喜您，创建成功。");
        }
        return ResponseResurt.error().msg("很抱歉，创建失败！");
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @ApiOperation(value = "修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID（主键）", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true),
            @ApiImplicitParam(name = "description", value = "描述", required = true),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @RequestMapping(value = "/updateRole", method = RequestMethod.GET)
    public ResponseResurt updateRole(Role role) {
        // 查询当前版本号
        long version = roleMapper.selectById(role.getRoleId()).getVersion();
        role.setVersion(version);
        // 执行更新操作
        int isUpdate = roleMapper.updateById(role);
        if (isUpdate > 0) {
            return ResponseResurt.success().msg("恭喜您，修改成功。");
        }
        return ResponseResurt.error().msg("很抱歉，修改失败！");
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "roleId", value = "角色ID（主键）", required = true)
    @RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
    public ResponseResurt deleteRole(Role role) {
        // 判断是否有用户正在使用该角色
        Integer isExist = userRoleMapper.existUserRole(role.getRoleId());
        if (isExist != null) {
            return ResponseResurt.error().msg("存在用户使用该角色，无法删除。");
        }
        // 执行更新操作（逻辑删除）
        int isUpdate = roleMapper.deleteRoleById(role);
        if (isUpdate > 0) {
            return ResponseResurt.success().msg("恭喜您，删除成功。");
        }
        return ResponseResurt.error().msg("很抱歉，删除失败！");
    }

    /**
     * 角色详情
     * @param role
     * @return
     */
    @ApiOperation(value = "角色详情")
    @ApiImplicitParam(name = "roleId", value = "角色ID（主键）", required = true)
    @RequestMapping(value = "/getRoleById", method = RequestMethod.GET)
    public ResponseResurt getRoleById(Role role) {
        role = roleMapper.selectById(role.getRoleId());
        return ResponseResurt.success().data(role);
    }
}
